//
//  WBHomeViewController.swift
//  微博
//
//  Created by dong on 2017/11/15.
//  Copyright © 2017年 dong. All rights reserved.
//

import UIKit

//全局常量，尽量使用private修饰，否则到处都可以访问
private let cellId = "cellId"

class WBHomeViewController: WBBaseViewController {
    
    ///微博数据数组
    lazy var listViewModel = WBStatusListViewModel()
    
    ///加载数据
    override func loadData() {
        
        print("准备刷新最后一条\(self.listViewModel.statusList.last?.text)")
        

        listViewModel.loadStatus(pullup: self.isPullup){ (isSuccess, hasMorePullup) in
            
            print("加载数据结束")
            //结束刷新控件
            self.refreshControl?.endRefreshing()
            //恢复上拉刷新标记
            self.isPullup = false
            
            //刷新表格
            if hasMorePullup {
                
            self.tableView?.reloadData()
                
            }
        

        }
    }
    func showFriends(){
        
        let vc = WBDemoViewController()
        
        //vc.hidesBottomBarWhenPushed = true
        //push显示测试
        //push动作时nav做的
        navigationController?.pushViewController(vc, animated: true)
        
    }

}

// Mark:表格数据源方法,具体的数据源方法实现，不需要super
extension WBHomeViewController{
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        //1.取cell
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        
        //2.设置cell
        cell.textLabel?.text = listViewModel.statusList[indexPath.row].text
        
        //3.返回cell
        return cell
    }
}

// Mark 设置界面
extension WBHomeViewController{
    //重写父类
    override func setupTableView() {
         super.setupTableView()
        //设置导航栏按钮
      
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        
        //注册原型cell
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        
        setupNavTitle()
    }
    
    ///设置导航栏标题
    private func setupNavTitle() {
        
        let title = WBNetworkManager.shared.userAccount.screen_name
        let button = WBTitleButton(title: title)

        navItem.titleView = button
        
        button.addTarget(self, action: #selector(clickTitleButton), for: .touchUpInside)
    }
    
    @objc func clickTitleButton(btn: UIButton) {
        
        //设置选中状态
        btn.isSelected = !btn.isSelected
    }
    
}
